<template>
  <div>
    <div style="margin-bottom: 15px;">
      <!-- 顶部添加数据 -->
      <span>客户编号：</span>
      <el-input v-model="params.cno" style="width: 150px; margin-right: 15px;" placeholder="请输入客户编号"></el-input>
      <span>业务编号：</span>
      <el-input v-model="params.bno" style="width: 150px; margin-left: 5px; margin-right: 15px;" placeholder="请输入业务编号"></el-input>
      <span>业务状态：</span>
      <el-select v-model="params.status" clearable placeholder="请选择查询的工单状态" style="margin-left: 5px;">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button type="warning" style="margin-left: 10px;" @click='searchWorkOrder()'>查询</el-button>
      <el-button type="warning" style="margin-left: 10px;" @click='reset()'>清空</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click='insert()'>新增</el-button>
    </div>

    <div> 
      <!-- 引入表格 -->
      <el-table
        :data="tableData"
        :border="true"
        max-height="600">
        style="width: 100%">
        <!-- 表格内容 -->
        <el-table-column fixed prop="ono" label="工单编号" width="100"></el-table-column>
        <el-table-column prop="cno" label="客户编号" width="130"></el-table-column>
        <el-table-column prop="bno" label="业务编号" width="100"></el-table-column>
        <el-table-column prop="status" label="工单状态" width="100"></el-table-column>
        <el-table-column prop="note" label="备注" width="200"></el-table-column>
        <el-table-column  label="操作" width="400">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-button type="success" @click="complete(scope.row.ono)">回单</el-button>
            <el-button type="warning" @click="cancel(scope.row.ono)">退单</el-button>
            
            <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.ono)">
                <el-button slot="reference" type="danger" style="margin-left: 5px;">删除</el-button>
              </el-popconfirm>
          </template>
          
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页显示 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]" 
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next"
          :total="total">
        </el-pagination>
      </div>

      <!-- 对话框 -->
       <div>
          <el-dialog title="请填写信息" :visible.sync="dialogFormVisible" width='40%'>
          <el-form :model="form">
            <el-form-item label="客户编号" label-width= "15%">
              <el-input v-model="form.cno" autocomplete="off" style="width:90%"></el-input>
            </el-form-item>
            <el-form-item label="业务编号" label-width= "15%">
              <el-input v-model="form.bno" autocomplete="off" style="width:90%"></el-input>
            </el-form-item>
            <el-form-item label="业务备注" label-width= "15%">
              <el-input v-model="form.note" autocomplete="off" style="width:90%"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click='submit()'>确定</el-button>
          </div>
          </el-dialog>
        </div>
  </div>
</template>

<script>
import request from '@/utils/request'


  export default {
    data() {
      return {
        params: {
          cno: '',
          bno: '',
          pageNum: 1,
          pageSize: 10
        },
        total:0,
        form:{},
        dialogFormVisible: false,
        tableData: [],
        options: [{
          value: 'CREATED',
          label: 'CREATED'
        }, {
          value: 'COMPLETED',
          label: 'COMPLETED'
        }, {
          value: 'CANCELLED',
          label: 'CANCELLED'
        }],
        status: ''
      }
    },

    // 页面加载时，在created中做一些事情
    created() {
      this.searchWorkOrder();
    },
    
    // 定义一些页面上控件触发的方法
    methods: {
      searchWorkOrder() {
        request.get("/workOrder/searchWorkOrder", {
          params: this.params
        }).then(res => {
          if (res.code === '0') {
            this.tableData = res.data.list;
            this.total = res.data.total;
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      },

      // 清空输入空内容 --> 查询所有
      reset() {
        this.params = {
          cno: '',
          bno: '',
          pageNum: 1,
          pageSize: 10
        }
        this.searchWorkOrder();
      },

      // 显示出对话框
      insert() {
        this.form = {};
        this.dialogFormVisible = true;
      },
      
      // 编辑内容，用于更新
      edit(obj) {
        this.form = obj;
        this.dialogFormVisible = true;
      },

      // 提交插入的内容
      submit() {
        request.post("/workOrder/insert", this.form).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.dialogFormVisible = false;
            this.searchWorkOrder();
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      },

      complete(ono) {
        request.post("/workOrder/complete/" + ono).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '回单成功',
              type: 'success'
            });
            this.searchWorkOrder();
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      },

      cancel(ono) {
        request.post("/workOrder/cancel/" + ono).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '退单成功',
              type: 'success'
            });
            this.searchWorkOrder();
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      },

      // 删除内容
      del(ono) {
        request.delete("/workOrder/delete/" + ono).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.searchWorkOrder();
            this.tableData = this.tableData.filter(item => item.ono !== ono);
            this.total -= 1;  
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      },

      // 每页条数改变触发
      handleSizeChange(pageSize) {
        // 改变每页显示条数
        this.params.pageSize = pageSize; 
        // 再做一次查询
        this.searchWorkOrder();
      },

      // 页码改变触发
      handleCurrentChange(currentPage) {
        this.params.pageNum = currentPage;
        this.searchWorkOrder();
      },

    }
  }

</script>